
<%@page import="org.apache.commons.fileupload.*"%>
<%@page import="org.apache.commons.fileupload.servlet.*"%>
<%@page import="org.apache.commons.fileupload.disk.*" %>
<%@page import="org.apache.commons.fileupload.util.*" %>
<%@page import="org.apache.commons.fileupload.portlet.*" %>
<%@page import="org.apache.commons.io.output.*"%>
<%@page import="classes.Student"%>
<%@page import="java.util.*" %>
<%@page import="java.io.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<link rel="stylesheet" href="css/schooledin.css" type="text/css" />
<link rel="stylesheet" href="css/profile.css" type="text/css" />
<title>SchooledIn.COM</title>
</head>
<html>

<%
  String cmd              = request.getParameter("cmd") == null ? "" : request.getParameter("cmd");
  String sid			  = request.getParameter("sid");
  String localDirectory = System.getProperty("dbPath") + "images/";
  
  String filename         = "";
  String message          = "";


if( cmd.equals("upload") ) {
    
  try {
    
    if( ServletFileUpload.isMultipartContent(request) )  {
	    FileItemFactory factory = new DiskFileItemFactory();
	    ServletFileUpload upload = new ServletFileUpload(factory);
	    List items = upload.parseRequest(request);
	    
	    Iterator iter = items.iterator();
	    FileItem fileitem = null;
	    if( iter.hasNext() ) {
	        fileitem = (FileItem)iter.next();
	    }
	    
	    filename = fileitem.getName();
	    
	    if( fileitem.getSize() > 0 && !filename.equals("") ) {	        
	        File saved = new File(localDirectory+filename);
	        fileitem.write(saved);    
	    }
    }
  }
  catch( Exception e ) {
      message += e.getMessage();
  }
  
  Student s = new Student(Integer.parseInt(sid));
  s.updatePicture(filename);
}

%>

<script>

<%
if( cmd.equals("upload") && message.equals("") ) {
%>
    alert('Image uploaded');
    window.close();
    window.opener.location.reload();
<%
}
else if( cmd.equals("upload") && !message.equals("") ) {
%>
    alert("Error uploading Image");
<%
}
%>

function checkSubmit() {

    var form = document.image_form;
    var image = document.getElementById("imageFile");
    
    if( image.value == '' )
        alert('An image is required');
    else {
        form.action = 'ImagePopup.jsp?cmd=upload&sid=<%=sid%>';
        form.submit();
    }
}

</script>

<body>
<table border="0" cellspacing="5" cellpadding="0" width="95%">
  <form method="POST" name="image_form" enctype="multipart/form-data" action="ImagePopup.jsp">
  <tr>
    <td align="LEFT" colspan="2" class="product"><br>
      <span class="title">Upload Profile Image</span>
    </td>
  </tr>
  <%
if (!message.equals("")) {
%>
  <tr>
    <td align="CENTER" colspan="2" style="background-color: #FFCCCC; padding: 8px;">
      <b>Error:</b><br>
      <%=message%>
    </td>
  </tr>
<%
}
%>
  <tr>
    <td align="center" colspan="2"><br>
      <input id="imageFile" type="file" name="file"/>
    </td>
  </tr>
  <tr>
    <td align="CENTER" colspan="2"><br>
      <span class="upload" onclick="checkSubmit()">Upload</span>
      <span class="upload" onclick="window.close()">Cancel</span>      
    </td>
  </tr>
  </form>
</table>

</body>
</html>